<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ErrorCode
 *
 * @author Administrator
 *         $valid =    $this->valid->check("username","18105311561");
*
 */
class ValidTools {
    private static $_instance;
    private $log;
    private static $rule;
    private function __construct() {
        
    }

    public static function getInstance() {
        if (self::$_instance === null) {
                 self::$_instance=new ValidTools();//方式一
        }
        return self::$_instance;
    }
    
    private  function get_variable_name(&$var, $scope = null) {
        $scope = $scope == null ? $GLOBALS : $scope; // 如果没有范围则在globals中找寻  
        // 因有可能有相同值的变量,因此先将当前变量的值保存到一个临时变量中,然后再对原变量赋唯一值,以便查找出变量的名称,找到名字后,将临时变量的值重新赋值到原变量  
        $tmp = $var;
        $var = 'tmp_value_' . mt_rand();
        $name = array_search($var, $scope, true); // 根据值查找变量名称  
        $var = $tmp;
        return $name;
    }

    public function checkAll($func,$array,$notNull =  true){
        
        foreach ($array as $key => $param){
            
            if($notNull == FALSE && $param == ""){
                continue;
            }
            $res = $this->check($func,$param);
            if($res == FALSE){
                return $key;
                break;
            }
        }
        return TRUE;
    }

    //合法性检验方法
     public  function check($func, $param) {
        $this->func = $func;
         $this->log = "\r\nvalidate:: $this->func | PARAM = $param \r\n";
        $functionName = "validate".$func;
        $ruleObj =    self::$rule;
        $rule = $ruleObj->$func;
        
        if($rule == ""){
              $this->log .= "END  $this->func is not exist \r\n";
              return false;
        }
  
       if(isset($rule->empty)){
           $res = $this->isEmpty($param,$func);
           if(!$res){
               return false;
           }
       }
       if(isset($rule->regex)){
           $res = $this->regex($param,$rule->regex);
             if(!$res){
               return false;
           }
       }
               
       if(isset($rule->min) ||isset($rule->max)  ){
            $res = $this->length($param,$rule->min,$rule->max);
            if(!$res){
                  return false;
            }
       }
 
       return true;
    }

    
    public function isEmpty($param){
        
            if($param =="" || $param == null ){
                $this->log .= "validate:: $this->func $param IS NULL VALIDATE FALSE \r\n";
                return false;
            }else{
                 $this->log .= "validate:: $this->func $param NOT NULL VALIDATE TRUE \r\n";
                return true;
            }
    }

    public function regex($param, $reg) {
        $validate = preg_match($reg, $param);
        if ($validate == 0) {
               $this->log .= "validate:: $this->func $reg Regx FALSE \r\n";
            return false;
        }
                $this->log .= "validate::  $this->func $reg Regx TRUE \r\n ";
        return true;
    }

    public function getLog(){
        
        return $this->log;
    }
    static function configure($path){
            $xml = simplexml_load_file($path); 
            self::$rule = $xml;
    }
 
        
    public function length($param,$min,$max){
        $len = strlen($param);
        if($min == "" && $max ==""){
            $this->log .= "validate::length NO LENGTH LIMIT TRUE \r\n";
            return true;
        }
        
        if($min!="" && $max==""){
            if($len < $min ){
                $this->log .= "validate:: $this->func length MIN=$min PARAM-LEN=$len VALIDATE FALSE \r\n";
                return FALSE;
            }else{
                $this->log .= "validate:: $this->func length MIN=$min PARAM-LEN=$len VALIDATE TRUE \r\n";
                return true;
            }
        }
        if($min!="" && $max!=""){
              if($len < $min || $len >  $max){
                $this->log .= "validate:: $this->func length MIN=$min MAX= $max PARAM-LEN=$len VALIDATE FALSE \r\n";
                return FALSE;
            }else{
                $this->log .= "validate:: $this->func length MIN=$min  MAX= $max  PARAM-LEN=$len VALIDATE TRUE \r\n";
                return true;
            }
        }
        
        if($min=="" && $max!=""){
            if($len > $max ){
                $this->log .= "validate:: $this->func length MAX=$max PARAM-LEN=$len VALIDATE FALSE \r\n";
                return FALSE;
            }else{
                $this->log .= "validate:: $this->func length MAX=$max PARAM-LEN=$len VALIDATE TRUE \r\n";
                return true;
            }
        }
    }
       
    
}
